package com.tangfh.nacos.demo.provider;

import com.tangfh.nacos.demo.interfaces.StockService;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @Program: springcloud-alibaba-nacos-dubbo-demo
 *
 * @Description:
 *
 * @Author: tangfh
 *
 * @Create: 2024-08-25 17:22
 */
@SpringBootApplication
@EnableDubbo
public class ProviderApplication implements CommandLineRunner {

    public static void main(String[] args) {
//        SpringApplication.run(ProviderApplication.class, args);
        // 创建SpringApplication实例
        SpringApplication application = new SpringApplication(ProviderApplication.class);
        // 添加自定义的ApplicationContextInitializer
        application.addInitializers(context -> {
            // 获取Environment对象
            Environment env = context.getEnvironment();
            // 从Environment中读取"spring.application.name"属性值
            String appName = env.getProperty("spring.application.name");
            String filePath = System.getProperty("user.home") + File.separator + ".dubbo" +File.separator + appName;
            // 修改dubbo的本地缓存路径，避免缓存冲突
            System.setProperty("dubbo.meta.cache.filePath", filePath);
            System.setProperty("dubbo.mapping.cache.filePath",filePath);
        });
        //启动应用
        application.run(args);

    }

    @Resource
    private StockService stockService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(stockService.reduceStock());
    }
}
